package com.zkingsoft.web.async;

import com.matrix.async.core.TaskHandler;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.web.bean.VisitorLog;
import com.zkingsoft.web.dao.VisitorLogDao;
import com.zkingsoft.web.interceptor.AddressUtils;

import java.util.Map;

/**
 * 功能描述: 异步设置访问地址
 *
 * @param:
 * @return:
 * @auther: wangpei
 * @email
 * @date: 2019/2/13 15:41
 */
public class SynchronizationSetAddress implements TaskHandler {
    /**
     * 功能描述:异步设置访问地址
     *
     * @param:
     * @return:
     * @auther: wangpei
     * @email
     * @date: 2019/2/13 15:43
     */
    @Override
    public boolean execute(Map<String, String> dataMap) {
        LogUtil.info("异步设置访问地址开始!!!!!!");
        VisitorLogDao visitorLogDao = WebUtil.getApplicationContext().getBean(VisitorLogDao.class);
        String visitorLogId = dataMap.get("visitorLogId");
        VisitorLog visitorLog = new VisitorLog();
        /* ip需要采用异步的方式去分析 */
        String countryStr = null;
        try {
            LogUtil.info("进入了try");
            //113.246.152.178  dataMap.get("ip")
            countryStr = AddressUtils.getAddressByIp("113.246.152.178");
            LogUtil.info("countryStr------>"+countryStr);
            if (StringUtils.isNotBlank(countryStr)) {
                String region = "";
                String city = "";
                String country = "";
                String isp = "";
                String area = "";
                if (StringUtils.isNotBlank(countryStr)) {
                    String[] countrys = countryStr.split(" ");
                    if (countrys.length > 0) {
                        country = countrys[0];
                    }
                    if (countrys.length > 1) {
                        region = countrys[1];
                    }
                    if (countrys.length > 2) {
                        city = countrys[2];
                    }
                    if (countrys.length > 3 || countrys.length == 3) {
                        isp = countrys[3];
                    }
                }
                LogUtil.info("国家： " + country);
                LogUtil.info("省份: " + region);
                LogUtil.info("城市： " + city);
                LogUtil.info("互联网服务提供商： " + isp);
                visitorLog.setCity(city);
                visitorLog.setProvince(region);
                visitorLog.setArea(country);
                visitorLog.setInfo1(isp);
                visitorLog.setId(visitorLogId);
                int i = visitorLogDao.updateByModel(visitorLog);
                if (i > 0) {
                    LogUtil.info("异步设置访问数据修改成功");
                } else {
                    LogUtil.info("异步设置访问数据修改失败");
                }
            }
        } catch (Exception e) {
            LogUtil.info("异步设置访问地址失败!!!!!!" + e);
            return false;
        }
        LogUtil.info("异步设置访问地址结束!!!!!!");
        return true;
    }

    @Override
    public void onError(Map<String, String> dataMap, Exception e) {

    }
}
